package cn.bdqn.pn.service.user.impl;


import cn.bdqn.pn.pojo.order.MainOrder;
import cn.bdqn.pn.pojo.user.Role;
import cn.bdqn.pn.pojo.user.User;
import cn.bdqn.pn.reposity.user.UserDao;
import cn.bdqn.pn.service.user.UserService;
import cn.bdqn.pn.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    /**
     * 分页查询用户信息
     */
    public Page<User> selectUserInfo(User user, Integer pageNo, Integer pageSize) throws SQLException {
        Specification<User> specification=new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();
                if (user.getStatus()!=null&&user.getStatus()!=-1){
                    System.out.println("================userservice"+user.getStatus());
                    list.add(cb.equal(root.get("status"),user.getStatus()));
                }
                if (user.getNickName()!=null&&!user.getNickName().equals("")){
                    System.out.println("================userservice"+user.getNickName());
                    list.add(cb.like(root.get("nickName"),"%"+user.getNickName()+"%"));
                }
                if (user.getTelephone()!=null&&!user.getTelephone().equals("")){
                    System.out.println("================userservice"+user.getTelephone());
                    list.add(cb.equal(root.get("telephone"),user.getTelephone()));
                }
                Predicate[] predicates =new Predicate[list.size()];
                return cb.and(list.toArray(predicates));
            }
        };
        Pageable pageable=new PageRequest(pageNo,pageSize);
        return userDao.findAll(specification,pageable);
    }

    @Override
    /**
     * 根据id查询用户详细信息
     */
    public User selectUserDetails(Integer userId) {
        return userDao.findOne(userId);
    }

    @Override
    /**
     * 修改用户状态
     */
    public Integer enableUser( Integer status,Integer userid) {
        User user=userDao.findOne(userid);
        user.setStatus(status);
        User user1=userDao.save(user);
        if(user1!=null){
            return 1;
        }
        return 0;
    }

    @Override
    /**
     * 查询用户注册量
     */
    public Integer userCount(Integer uesrId) {

        return null;
    }





    /**
     * 查询用户总量
     */
    @Override
    public Long getUsers() {
        return userDao.count();
    }

    /**
     * 获取今日新增会员
     * @return
     */
    @Override
    public Integer VIPByToday() {
        System.out.println(TimeUtil.dangqian());
        return userDao.VIPByToday(TimeUtil.dangqian());
    }
}
